package com.itheima.controller;

import com.itheima.MessageConstant;
import com.itheima.pojo.PictureSort;
import com.itheima.service.PictureSortService;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.PictureSortVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 图片分类相关接口
 */
@RestController
@RequestMapping("/pictureSort")
@Api(tags = "图片分类相关接口")
public class PictureSortController {
    @Autowired
    private PictureSortService pictureSortService;

    @ControllerMonitor
    @PostMapping("/add")
    @ApiOperation("添加图片分类")
    public String add(@RequestBody PictureSortVO pictureSortVO){
        //因为置顶权重为101,所有在添加和修改的时候权重不能大于100[前台做校验]
        if (pictureSortVO.getSort()>100){
            return ResultUtil.result(401, MessageConstant.SORT_SET_FAIL);
        }
        try {
            pictureSortService.add(pictureSortVO);
            return ResultUtil.result(200, MessageConstant.PICTURESORT_ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURESORT_ADD_FAIL);
        }
    }

    @ControllerMonitor
    @PostMapping("/delete")
    @ApiOperation("删除图片分类")
    public String delete(@RequestBody PictureSortVO pictureSortVO){
        if (pictureSortVO.getUid()==null || "".equals(pictureSortVO.getUid())){
            return ResultUtil.result(401, MessageConstant.ADD_UID);
        }
        try {
            pictureSortService.delete(pictureSortVO);
            return ResultUtil.result(200, MessageConstant.PICTURESORT_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURESORT_DELETE_FAIL);
        }
    }

    @ControllerMonitor
    @PostMapping("/getList")
    @ApiOperation("获取所有的图片分类信息")
    public String getList(@RequestBody PictureSortVO pictureSortVO)  {
        //设置默认当前页
        if (pictureSortVO.getCurrentPage()==null || "".equals(pictureSortVO.getCurrentPage())){
            pictureSortVO.setCurrentPage(0);
        }
        //设置默认每页条数
        if (pictureSortVO.getPageSize()==null || "".equals(pictureSortVO.getPageSize())){
            pictureSortVO.setPageSize(10);
        }
        try {
            List<PictureSort> list = pictureSortService.getList(pictureSortVO);
            return ResultUtil.result(200,list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURESORT_FINDALL_FAIL);
        }
    }

    @ControllerMonitor
    @PostMapping("/edit")
    @ApiOperation("修改图片分类")
    public String edit(@RequestBody PictureSortVO pictureSortVO){
        //因为置顶权重为101,所有在添加和修改的时候权重不能大于100[前台做校验]
        if (pictureSortVO.getSort()>100){
            return ResultUtil.result(401, MessageConstant.SORT_SET_FAIL);
        }
        if (pictureSortVO.getUid()==null || "".equals(pictureSortVO.getUid())){
            return ResultUtil.result(401, MessageConstant.ADD_UID);
        }
        try {
            pictureSortService.edit(pictureSortVO);
            return ResultUtil.result(200, MessageConstant.PICTURESORT_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURESORT_EDIT_FAIL);
        }
    }

    @ControllerMonitor
    @PostMapping("/getPictureSortByUid")
    @ApiOperation("根据id获取图片分类")
    public String getPictureSortByUid(@RequestBody PictureSortVO pictureSortVO){
        if (pictureSortVO.getUid()==null || "".equals(pictureSortVO.getUid())){
            return ResultUtil.result(401, MessageConstant.ADD_UID);
        }
        try {
            PictureSort pictureSort = pictureSortService.getPictureSortByUid(pictureSortVO);
            return ResultUtil.result(200,pictureSort);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURESORT_FIND_FAIL);
        }
    }

    @ControllerMonitor
    @PostMapping("/stick")
    @ApiOperation("图片分类置顶")
    public String stick(@RequestBody PictureSortVO pictureSortVO){

        try {
            List<PictureSort> list = pictureSortService.stick(pictureSortVO);
            return ResultUtil.result(200,list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, MessageConstant.PICTURESORT_EDIT_FAIL);
        }
    }
}
